﻿Imports System.Activities
Imports System.ComponentModel
Imports System.Activities.Presentation.PropertyEditing
Imports System.Activities.Presentation.Metadata
Imports System.Drawing

Public Class FileCopyActivity
    Inherits AsyncCodeActivity
    Implements IRegisterMetadata

    Public Property Target As InArgument(Of String)
    Public Property DestinationFolder As InArgument(Of String)
    Public Property Result As OutArgument(Of Boolean)

    Private Delegate Function asyncFileCopyDelegate(ByVal original As String, ByVal target As String) As Boolean

    Public Sub New()
        Me.DisplayName = My.Resources.FileCopyDisplayName
    End Sub

    Protected Overrides Function BeginExecute(ByVal context As System.Activities.AsyncCodeActivityContext, ByVal callback As System.AsyncCallback, ByVal state As Object) As System.IAsyncResult
        Dim source = context.GetValue(Me.Target)
        Dim dest = context.GetValue(Me.DestinationFolder)
        Dim asyncExecute = New asyncFileCopyDelegate(AddressOf FileCopy)
        context.UserState = asyncExecute

        Return asyncExecute.BeginInvoke(source, dest, callback, state)
    End Function

    Protected Overrides Sub EndExecute(ByVal context As System.Activities.AsyncCodeActivityContext, ByVal result As System.IAsyncResult)
        Dim asyncExecute = TryCast(context.UserState, asyncFileCopyDelegate)
        Dim copyResult = asyncExecute.EndInvoke(result)

        context.SetValue(Me.Result, copyResult)
    End Sub

    Private Function FileCopy(ByVal original As String, ByVal target As String) As Boolean
        Dim copyResult = False
        Try
            target = System.IO.Path.Combine(target, System.IO.Path.GetFileName(original))
            My.Computer.FileSystem.CopyFile(original, target, True)
            copyResult = True
        Catch ex As Exception

        End Try
        Return copyResult
    End Function

    Public Sub Register() Implements System.Activities.Presentation.Metadata.IRegisterMetadata.Register
        Dim build As New AttributeTableBuilder

        'FileCopy Activity
        build.AddCustomAttributes(GetType(FileCopyActivity),
                                              New ToolboxBitmapAttribute(GetType(FileCopyActivity), "FileCopy.png"))
        build.AddCustomAttributes(GetType(FileCopyActivity), "Target",
                                              New RequiredArgumentAttribute,
                                              New EditorAttribute(GetType(FilepathPropertyEditor), GetType(DialogPropertyValueEditor)),
                                              New CategoryAttribute(My.Resources.FileCopyCategory),
                                              New DisplayNameAttribute(My.Resources.FileCopyTargetDisplay),
                                              New DescriptionAttribute(My.Resources.FileCopyTargetDescription))
        build.AddCustomAttributes(GetType(FileCopyActivity), "DestinationFolder",
                                              New RequiredArgumentAttribute,
                                              New EditorAttribute(GetType(FolderpathPropertyEditor), GetType(DialogPropertyValueEditor)),
                                              New CategoryAttribute(My.Resources.FileCopyCategory),
                                              New DisplayNameAttribute(My.Resources.FileCopyDestinationDisplay),
                                              New DescriptionAttribute(My.Resources.FileCopyDestinationDescription))
        build.AddCustomAttributes(GetType(FileCopyActivity), "Result",
                                              New CategoryAttribute(My.Resources.FileCopyCategory),
                                              New DisplayNameAttribute(My.Resources.FileCopyResultDisplay),
                                              New DescriptionAttribute(My.Resources.FileCopyResultDescription))

        MetadataStore.AddAttributeTable(build.CreateTable)
    End Sub
End Class
